(function (window, $, undefined) {
  'use strict';

  var dictData = {
    orderStatus: {
      '1': '未审核',
      '2': '已审核（未开票）',
      '3': '已开票（未邮寄）',
      '4': '已邮寄',
      '5': '未通过审核'
    },
    billingType: {
      '1': '纸质发票',
      '2': '电子发票'
    },
    title: {}
  };

  var historyData = {}

  main(function () {

    var $tableInvoice = $('#table-history');
    util.jqGrid($tableInvoice, {
      url: ajaxURL.COUPON + '/billing/select',
      datatype: 'json',
      autowidth: true,
      height: $(window).height() - 350,
      shrinkToFit: false,
      colModel: [
        { name: 'kid', key: true, hidden: true },
        { label: '编号', name: 'kid', width: 80 },
        {
          label: '发票单号', name: 'billingNo', width: 150, formatter: function (v, o, r) {
            return '<a href="javascript:;" class="view" data-id="' + r.kid + '">' + v + '</a>';
          }
        },
        { label: '发票类别', name: 'billingType', width: 100, formatter: function (v, o, r) {
          return dictData.billingType[v] || '';
        },
      },
        { label: '发票金额', name: 'money', width: 120, formatter: function (v, o, r) {
          return v/100 + ' 元';
        },
      },
        { label: '发票张数', name: 'billingNumber', width: 50 },
        { label: '金额总数', name: 'amount', width: 100, formatter: function (v, o, r) {
          return v/100 + ' 元';
        },
      },
        { label: '发票抬头', name: 'title', width: 100, formatter: function (v, o, r) {
          return dictData.title[v] || '';
        },
      },
        { label: '纳税人识别号', name: 'idNumber', width: 120 },
        { label: '姓名', name: 'userName', width: 100 },
        { label: '电话', name: 'phone', width: 100 },
        { label: '地址', name: 'address', width: 200, formatter: function (v, o, r) {
          return r.city + ' '  + v;
        },
      },
        { label: '邮寄单号', name: 'mailNumber', width: 120 },
        {
          label: '发票状态', name: 'orderStatus', width: 120, formatter: function (v, o, r) {
            return dictData.orderStatus[v] || '';
          },
        }
      ],
      pager: '#pager-history',
      loadComplete: function (res) {
        historyData = {};
        if (res && res.data) {
          $.each(res.data, function (_, item) {
            historyData[item.kid] = item;
          });
        }
      },
    });

    util.selectHtml($('[name="orderStatus"]'), dictData.billingType, '', '', '全部');

    // 下单时间
    laydate.render({ elem: '#dealDate' });

    var $layerInvoice = $('#layer-history-view')
    var $layerMail = $('#layer-history-mail')
    var $formMail = $('#form-mail')

    // 处理字典
    util.getDict('invoiceCategory', 'invoiceCategory').then(function (data) {
      util.selectHtml('[name="title"]', data, 'value', 'name', '请选择');
      $.each(data, function (_, item) {
        dictData.title[item.value] = item.name;
      });
    });

    util.selectHtml($('[name="orderStatus"]'), dictData.orderStatus, '', '', '请选择');
    util.selectHtml($('[name="billingType"]'), dictData.billingType, '', '', '请选择');
    util.selectHtml($('[name="orderStatus"]'), dictData.billingType, '', '', '全部');

    // 查看操作
    $tableInvoice.on('click', '.view', function () {
      var $this = $(this);
      var id = $this.data('id');

      var data = historyData[id];

      data.orderTime = util.getDate(data.orderTime);
      util.fillForm($layerInvoice, data)

      util.layerOpen({
        title: '发票详情',
        content: $layerInvoice,
        area: ['1000px', '']
      });
    });



  });


})(window, jQuery);
